﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace TTBox
{
    public partial class DispatchAskPanel : UserControl
    {


        public DispatchAskPanel()
        {
            InitializeComponent();
        }

        int m_nIndex = 0;
        int m_nTimeCount = 0;

        string m_strButtonMins = "005010015020025030035040045";

        string m_strLastCMD = "";
        public string m_strLastOrderID = "";
        public int m_nLastMins = 5;

        DateTime m_dtButtonEnable;
        public void initial(string arg_strCMD)
        {
            SoundManager.getInstance().playJobAsk();
            m_dtButtonEnable = System.DateTime.Now.AddSeconds(1.2);
            LogManager.getInstance().Log("initial DAP");
            m_nTimeCount = 0;
            this.timer_Timeout.Enabled = true;

            m_nIndex = 0;

            switch (arg_strCMD[3])
            {
                case '1':
                    m_strButtonMins = StoreData.getInstance().RealTimeButtonMins;
                    break;
                case '2':
                    m_strButtonMins = StoreData.getInstance().SupportButtonMins;
                    break;
                case '5':
                case '4':
                case '3':
                    m_strButtonMins = "X";
                    break;
            }

            m_strLastCMD = arg_strCMD;
            m_strLastOrderID = arg_strCMD.Substring(3, 15);
            this.textBox_Msg.Text = arg_strCMD.Substring(19);

            LogManager.getInstance().Log("OrderID:" + m_strLastOrderID);
            setupButtons();

            this.button_F1.Enabled = false;
            this.button_F2.Enabled = false;
            this.button_F3.Enabled = false;
            this.button_F4.Enabled = false;

        }
        public void setupButtons()
        {
            if (m_strButtonMins !=  "X")
            {

                setButtonMins(this.button_F1,  (m_nIndex));
                setButtonMins(this.button_F2,  (m_nIndex + 1));
                setButtonMins(this.button_F3,  (m_nIndex + 2));
                this.button_S.Visible = true;
            }
            else
            {
                this.button_F1.Text = "承接";
                this.button_S.Visible = false;
                this.button_F2.Visible = false;
                this.button_F3.Visible = false;

            }


        }

        private void setButtonMins(Button arg_btn, int arg_nIndex)
        {
            if (m_strButtonMins.Length >= 3 * (arg_nIndex+1))
            {
                arg_btn.Visible = true;
                string strMins = m_strButtonMins.Substring(arg_nIndex * 3, 3);
                arg_btn.Text = strMins.TrimStart('0') + "分";
                arg_btn.Tag = strMins;
                LogManager.getInstance().Log("set Button:" + strMins);
            }
            else
            {
                arg_btn.Visible = false;
            }


        }

        private void button_F1_Click(object sender, EventArgs e)
        {
            disableTimeOut();
            if (m_strButtonMins != "X")
            {

                Button bTemp = (Button)sender;
                this.Tag = "80" + bTemp.Tag;

                this.m_nLastMins = System.Convert.ToInt32(bTemp.Tag);

                Form_Main.Main.ChangeToMainScreen();
                CmdTalker.getInstance().SendServerAndWaitResponse(Form_Main.Main.getCurrStatusChar() + "80" + bTemp.Tag + m_strLastOrderID, 10, "MOK", true);

            }
            else
            {
                Form_Main.Main.ChangeToMainScreen();
                CmdTalker.getInstance().SendServerAndWaitResponse(Form_Main.Main.getCurrStatusChar() + "88" + m_strLastOrderID, 10, "MOK", true);
            }
                

        }

        private void disableTimeOut()
        {
            m_nTimeCount = 0;
            this.timer_Timeout.Enabled = false;
        }

        private void button_F4_Click(object sender, EventArgs e)
        {
            disableTimeOut();
            this.Tag = "89";
            Form_Main.Main.ChangeToMainScreen();
            CmdTalker.getInstance().SendServer(Form_Main.Main.getCurrStatusChar() + "89" + m_strLastOrderID);


        }



        private void button_S_Click(object sender, EventArgs e)
        {
            m_nIndex += 3;

            if (m_strButtonMins.Length < 3 * (m_nIndex + 1))
                m_nIndex = 0;
            setupButtons();
        }


        private void timer_Timeout_Tick(object sender, EventArgs e)
        {
            m_nTimeCount += 1;
            if (m_nTimeCount > StoreData.getInstance().WaitTimeoutSecs)
            {
                this.timer_Timeout.Enabled = false;
                this.Tag = "90";
                Form_Main.Main.ChangeToMainScreen();
                CmdTalker.getInstance().SendServer(Form_Main.Main.getCurrStatusChar() + "90" + m_strLastOrderID);
                m_nTimeCount = 0;
            }

            SoundManager.getInstance().playJobAsk();

            if (!this.button_F1.Enabled && System.DateTime.Now > m_dtButtonEnable)
            {
                this.button_F1.Enabled = true;
                this.button_F2.Enabled = true;
                this.button_F3.Enabled = true;
                this.button_F4.Enabled = true;


            }
        }


    }
}
